﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UserControlLib.Enums;
using UserControlLib.Models;

namespace UserControlLib
{
    /// <summary>
    /// DisplayLogPanel.xaml 的交互逻辑
    /// </summary>
    public partial class DisplayLogPanel : UserControl
    {
        public DisplayLogPanel()
        {
            InitializeComponent();
            InitializeTabItems();
        }

        public static readonly DependencyProperty TabItemsSourceProperty =
            DependencyProperty.Register("TabItemsSource", typeof(Dictionary<string, IEnumerable<object>>), typeof(DisplayLogPanel),
                new PropertyMetadata(null, OnTabItemsSourceChanged));
        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(DisplayLogPanel), new PropertyMetadata(-1));

        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }
        public Dictionary<string, IEnumerable<object>> TabItemsSource
        {
            get => (Dictionary<string, IEnumerable<object>>)GetValue(TabItemsSourceProperty);
            set => SetValue(TabItemsSourceProperty, value);
        }

        public static readonly DependencyProperty LogFontSizeProperty =
    DependencyProperty.Register("LogFontSize", typeof(double), typeof(DisplayLogPanel),
        new PropertyMetadata(12.0));

        public double LogFontSize
        {
            get { return (double)GetValue(LogFontSizeProperty); }
            set { SetValue(LogFontSizeProperty, value); }
        }

        private static void OnTabItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var mainWindow = d as DisplayLogPanel;
            if (mainWindow != null)
            {
                mainWindow.InitializeTabItems();
            }
        }

        private void InitializeTabItems()
        {
            //var tabControl = new TabControl();

            LogTabControl.SetBinding(TabControl.SelectedIndexProperty, new Binding("SelectedIndex") { Mode = BindingMode.TwoWay });
            if (TabItemsSource != null)
            {
                foreach (var entry in TabItemsSource)
                {
                    var tabItem = new TabItem
                    {
                        FontSize = FontSize,
                        Header = entry.Key,
                        Content = CreateScrollViewerWithItemsControl(entry.Value)
                    };

                    var scrollViewer = (ScrollViewer)tabItem.Content;
                    scrollViewer.CanContentScroll = true;
                    scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
                    scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;

                    LogTabControl.Items.Add(tabItem);
                }

                LogTabControl.SelectedIndex = 0;
            }

            //this.Content = tabControl;
        }

        private UIElement CreateScrollViewerWithItemsControl(IEnumerable<object> itemsSource)
        {
            var scrollViewer = new ScrollViewer();

            var itemsControl = new ItemsControl
            {
                HorizontalContentAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                ItemsSource = itemsSource
            };

            var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
            stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);

            var dataTemplate = new DataTemplate
            {
                DataType = typeof(LogIteamModel)
            };

            var textBoxFactory = new FrameworkElementFactory(typeof(TextBox));
            textBoxFactory.Name = "txt";
            textBoxFactory.SetValue(TextBox.NameProperty, "txt");
            textBoxFactory.SetValue(TextBox.TextWrappingProperty, TextWrapping.Wrap);
            textBoxFactory.SetValue(TextBox.MarginProperty, new Thickness(0, 2, 0, 0));
            textBoxFactory.SetValue(TextBox.BackgroundProperty, Brushes.Transparent);
            //textBoxFactory.SetValue(TextBox.HeightProperty, 12.0);
            textBoxFactory.SetValue(TextBox.BorderThicknessProperty, new Thickness(0));
            // 绑定字体大小
            textBoxFactory.SetBinding(TextBox.FontSizeProperty, new Binding("LogFontSize") { Source = this });
            //textBoxFactory.SetValue(TextBox.FontSizeProperty, 12.0);
            textBoxFactory.SetValue(TextBox.ForegroundProperty, Brushes.DarkGray);
            textBoxFactory.SetValue(TextBox.IsReadOnlyProperty, true);
            textBoxFactory.SetBinding(TextBox.TextProperty, new Binding("LogMsg") { Mode = BindingMode.OneWay });
            var triggers = new List<TriggerBase>();
            var setter = new Setter(TextBox.ForegroundProperty, Brushes.Black);
            setter.TargetName = textBoxFactory.Name;
            var setter1 = new Setter(TextBox.ForegroundProperty, Brushes.Green);
            setter1.TargetName = textBoxFactory.Name;
            var setter2 = new Setter(TextBox.ForegroundProperty, Brushes.Red);
            setter2.TargetName = textBoxFactory.Name;
            triggers.Add(new DataTrigger
            {
                Binding = new Binding("LogTyp"),
                Value = LogType.None,
                Setters = { setter }
            });
            triggers.Add(new DataTrigger
            {
                Binding = new Binding("LogTyp"),
                Value = LogType.Friendly,
                Setters = { setter1 }
            });
            triggers.Add(new DataTrigger
            {
                Binding = new Binding("LogTyp"),
                Value = LogType.Vigilant,
                Setters = { setter2 }
            });

            // 将触发器添加到 DataTemplate
            foreach (var trigger in triggers)
            {
                dataTemplate.Triggers.Add(trigger);
            }

            dataTemplate.VisualTree = textBoxFactory;

            itemsControl.ItemTemplate = dataTemplate;

            scrollViewer.Content = itemsControl;

            return scrollViewer;
        }


    }
}
